/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*!
 * @file gpio_reg_access.c
 * @brief This file defines gpio register access functions
 */

/*******Includes***************************************************************/
#include <stddef.h>
#include "gpio_reg_access.h"

/*******Definitions************************************************************/

/*******Variables**************************************************************/

/*******Prototypes*************************************************************/

/*******Private Functions******************************************************/

/*******Code*******************************************************************/

/*******************************************************************************
 *** Function Name : GPIO_REG_Init
 *** Description   : Initializes the pins with the given configuration structure
 ******************************************************************************/
void GPIO_REG_Init(const gpio_pin_config_t* gpioConfig)
{
    ASSERT_PARAM(gpioConfig->base != NULL);
    ASSERT_PARAM(gpioConfig->pin < PORT_PIN_COUNT);

    /* Clear the interrupt flag of the pin */
    GPIO_REG_ClearPinIntFlag(gpioConfig->base, gpioConfig->pin);

    /* Configures the internal resistor for the pin from a port */
    GPIO_REG_SetPinPull(gpioConfig->base, gpioConfig->pin, gpioConfig->pullConfig);

    /* Configures the function for the pin from a port */
    GPIO_REG_SetPinFunc(gpioConfig->base, gpioConfig->pin, gpioConfig->mux);

    /* Enable or Disable the digital filter for the pin from a port */
    if (gpioConfig->digitalFilterRUN) {
        GPIO_REG_EnablePinDigitFltrRUN(gpioConfig->base, gpioConfig->pin);
    } else {
        GPIO_REG_DisablePinDigitFltrRUN(gpioConfig->base, gpioConfig->pin);
    }

    /* Configures the pin mode for the pin from a port */
    GPIO_REG_SetPinMode(gpioConfig->base, gpioConfig->pin, gpioConfig->pinMode);

    /* Read the pin Control Register */
    uint32_t regValue = gpioConfig->base->PCR[gpioConfig->pin];
    /* Configures the lock for the pin from a port */
    regValue = ((regValue & ~GPIO_PCR_LKC_Msk) | GPIO_PCR_LKC(gpioConfig->pinLock));
    /* Configures the drive strength for the pin from a port */
    regValue = ((regValue & ~GPIO_PCR_DSC_Msk) | GPIO_PCR_DSC(gpioConfig->driveStrength));
    /* Configures the slew rate for the pin from a port */
    regValue = ((regValue & ~GPIO_PCR_SRC_Msk) | GPIO_PCR_SRC(gpioConfig->slewRate));
    /* Write the configuration to the Control Register */
    gpioConfig->base->PCR[gpioConfig->pin] = regValue;

    /* Avoid to generate a interrupt by config pin interrupt finnaly when pin init level is high */
    GPIO_REG_SetPinInt(gpioConfig->base, gpioConfig->pin, gpioConfig->intConfig);

    /* Write the pin level */
    regValue = gpioConfig->base->ODR;

    if (gpioConfig->initLevel == GPIO_PIN_LEVEL_HIGH) {
        regValue |= (1UL << gpioConfig->pin);
    } else {
        regValue &= ~(1UL << gpioConfig->pin);
    }

    GPIO_REG_WritePins(gpioConfig->base, regValue);
}

/*******EOF********************************************************************/
